﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Nexus.IPConfiguration.Configurator.NetworkAdapterConfigs
{
    internal static class NetworkAdapterConfigsManager
    {
        #region fields
        private static string filePath;
        #endregion

        #region properties
        internal static List<NetworkAdapterConfiguration> Configs { get; private set; }
        #endregion

        #region constructor
        static NetworkAdapterConfigsManager()
        {
            string tmp = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string path = @"IP Configurator\NetworkConfigs.xml";
            filePath = Path.Combine(tmp, path);
            if (CheckConfigFileExistance())
            {
                ReadConfig();
            }
            else
            {
                CreateNewConfig();
            }
        }


        #endregion

        #region methods
        internal static void CreateNewConfig()
        {
            Configs = new List<NetworkAdapterConfiguration>();
            DirectoryInfo info = new DirectoryInfo(Path.GetDirectoryName(filePath));
            if (info.Exists == false)
            {
                info.Create();
            }
            SaveToFile();
        }

        internal static void SaveToFile()
        {
            FileInfo fileInfo = new FileInfo(filePath);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<NetworkAdapterConfiguration>));
            string xmlText;
            using (StringWriter writer = new StringWriter())
            {
                xmlSerializer.Serialize(writer, Configs);
                xmlText = writer.ToString();
            }
            try
            {
                using (StreamWriter writer = new StreamWriter(new FileStream(filePath, fileInfo.Exists ? FileMode.Truncate : FileMode.CreateNew)))
                {
                    writer.Write(xmlText);
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                string tmp = string.Empty;
            }
        }

        internal static void ReadConfig()
        {
            //Dodać obsługę błedu przy odczytywaniu configu
            string xmlText;
            using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open)))
            {
                xmlText = reader.ReadToEnd();
                reader.Close();
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<NetworkAdapterConfiguration>));
            using (StringReader reader = new StringReader(xmlText))
            {
                try
                {
                    Configs = (List<NetworkAdapterConfiguration>)xmlSerializer.Deserialize(reader);
                }
                catch (Exception ex)
                {
                    string tmp = string.Empty;
                }
            }
        }

        private static bool CheckConfigFileExistance()
        {
            FileInfo info = new FileInfo(filePath);
            return info.Exists;
        }

        internal static void AddNew(NetworkAdapterConfiguration newConfig)
        {
            Configs.Add(newConfig);
        }

        internal static void Delete(NetworkAdapterConfiguration current)
        {
            Configs.Remove(current);
        }

        internal static NetworkAdapterConfiguration GetConfig(string configName)
        {
            return Configs.Where(c => c.Name == configName).FirstOrDefault();
        }
        #endregion
    }
}
